package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //根据动态id和互动类型，查询互动数据
    public List<Comment> findComments(Integer page, Integer pagesize, String movementId,
                                      Integer commentType) {
        //sql:  xxxxx where publishId=? and commentType=?
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType);

        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((page - 1) * pagesize)
                .limit(pagesize);

        return mongoTemplate.find(query,Comment.class);
    }

    /**
     * 保存互动数据，返回总数
     *    ---保存评论数据，返回评论总数
     *    ---保存点赞数据，返回点赞总数
     *    ---保存喜欢数据，返回喜欢总数
     * @param comment
     */
    public Integer save(Comment comment) {
        //1、保存互动数据
        //1.1 获取当前动态的发布人
        ObjectId movementId = comment.getPublishId();
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        //1.2 设置到comment对象中
        comment.setPublishUserId(movement.getUserId());
        //1.3 调用mongotemplate保存到mongodb
        mongoTemplate.save(comment);

        //2、更新动态表中的评论总数字段(更新并获取最新值)
        //sql ： update movement set commentCount = commentCount+1 where id=?
        Query query = Query.query(Criteria.where("id").is(movementId));//更新条件

        Update update = new Update();//更新的字段
        if(comment.getCommentType() == 1) {
            update.inc("likeCount",1);
        }else if (comment.getCommentType() == 2) {
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //设置参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true); // 更新之后返回最新结果
        //更新并获取更新之后的最新结果
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);

        //返回值：如果评论，返回评论总数，如果点赞，返回点赞总数--
        if(comment.getCommentType() == 1) {
            return  modify.getLikeCount();
        }else if (comment.getCommentType() == 2) {
            return  modify.getCommentCount();
        }else {
            return  modify.getLoveCount();
        }
    }

    //根据动态id，用户id，和互动类型判断数据是否存在
    public Boolean hasComment(String movementId, Long userId, Integer commentType) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType)
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query,Comment.class); //数据存在返回true，数据不存在返回false
    }

    //删除互动数据，返回总数量
    public Integer delete(String movementId, Long userId, Integer commentType) {
        //1、删除Comment表中的数据
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType)
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        //2、修改Movement表中对应的互动字段的总数
        //update table set commentCount = commentCount - 1 where id=?
        Query movementQuery = Query.query(Criteria.where("id").is(new ObjectId(movementId)));
        Update update = new Update();
        if(commentType == 1) {
            update.inc("likeCount",-1);
        }else if (commentType == 2) {
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);//修改并获取修改之后的最新数据
        //返回总数量
        if(commentType == 1) {
            return  modify.getLikeCount();
        }else if (commentType == 2) {
            return  modify.getCommentCount();
        }else {
            return  modify.getLoveCount();
        }
    }

}
